<template>
  <div class="admin-dashboard">
    <!-- 管理员侧边栏 -->
    <div class="admin-sidebar">
      <div class="admin-sidebar-header">
        <h2>管理控制台</h2>
      </div>
      <ul class="admin-menu">
        <li>
          <router-link to="/admin/dashboard" class="menu-item" active-class="active">
            <span class="menu-icon">📊</span>
            <span class="menu-text">仪表盘</span>
          </router-link>
        </li>
        <li>
          <router-link to="/admin/users" class="menu-item" active-class="active">
            <span class="menu-icon">👥</span>
            <span class="menu-text">用户管理</span>
          </router-link>
        </li>
        <li>
          <router-link to="/admin/questions" class="menu-item" active-class="active">
            <span class="menu-icon">❓</span>
            <span class="menu-text">问答管理</span>
          </router-link>
        </li>
        <li>
          <router-link to="/admin/stories" class="menu-item" active-class="active">
            <span class="menu-icon">📖</span>
            <span class="menu-text">故事管理</span>
          </router-link>
        </li>
      </ul>
      <div class="admin-sidebar-footer">
        <button @click="handleLogout" class="logout-btn">退出登录</button>
      </div>
    </div>

    <!-- 管理员主内容区 -->
    <div class="admin-main-content">
      <div class="admin-header">
        <h1>仪表盘</h1>
        <div class="admin-user-info">
          <span>欢迎，{{ currentUser?.username }}</span>
        </div>
      </div>
      
      <div class="dashboard-stats">
        <div class="stat-card">
          <div class="stat-icon">👥</div>
          <div class="stat-content">
            <h3>{{ totalUsers }}</h3>
            <p>总用户数</p>
          </div>
        </div>
        <div class="stat-card">
          <div class="stat-icon">💎</div>
          <div class="stat-content">
            <h3>{{ memberUsers }}</h3>
            <p>会员用户</p>
          </div>
        </div>
        <div class="stat-card">
          <div class="stat-icon">❓</div>
          <div class="stat-content">
            <h3>{{ totalQuestions }}</h3>
            <p>问答数量</p>
          </div>
        </div>
        <div class="stat-card">
          <div class="stat-icon">📖</div>
          <div class="stat-content">
            <h3>{{ totalStories }}</h3>
            <p>故事数量</p>
          </div>
        </div>
      </div>

      <div class="recent-activities">
        <h2>最近活动</h2>
        <div class="activities-list">
          <div v-for="activity in recentActivities" :key="activity.id" class="activity-item">
            <span class="activity-time">{{ activity.time }}</span>
            <span class="activity-content">{{ activity.content }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { userStore } from '../../stores/user'

export default {
  name: 'DashboardView',
  data() {
    return {
      totalUsers: 0,
      memberUsers: 0,
      totalQuestions: 0,
      totalStories: 0,
      recentActivities: [],
      currentUser: null
    }
  },
  computed: {
    userState() {
      const state = userStore.getState()
      return state
    }
  },
  mounted() {
    // 检查用户是否已登录且是管理员
    this.checkAdminAccess()
    this.loadDashboardData()
  },
  methods: {
    checkAdminAccess() {
      if (!this.userState.isAuthenticated || !this.userState.currentUser.isAdmin) {
        this.$router.push('/admin/login')
      } else {
        this.currentUser = this.userState.currentUser
      }
    },
    loadDashboardData() {
      // 模拟数据
      this.totalUsers = 128
      this.memberUsers = 32
      this.totalQuestions = 456
      this.totalStories = 78
      
      this.recentActivities = [
        { id: 1, time: '10:30', content: '用户 "newuser" 注册了账号' },
        { id: 2, time: '昨天', content: '用户 "testuser" 发布了新问题' },
        { id: 3, time: '昨天', content: '用户 "memberuser" 升级为会员' },
        { id: 4, time: '2天前', content: '用户 "storyteller" 分享了新故事' }
      ]
    },
    handleLogout() {
      userStore.logout()
      this.$router.push('/admin/login')
    }
  }
}
</script>

<style scoped>
.admin-dashboard {
  display: flex;
  min-height: 100vh;
  background-color: #f5f7fa;
}

/* 管理员侧边栏 */
.admin-sidebar {
  width: 250px;
  background-color: #2c3e50;
  color: white;
  padding: 20px 0;
}

.admin-sidebar-header {
  padding: 0 20px 30px;
  border-bottom: 1px solid #34495e;
}

.admin-sidebar-header h2 {
  margin: 0;
  font-size: 18px;
}

.admin-menu {
  list-style: none;
  padding: 0;
  margin: 20px 0;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  color: #bdc3c7;
  text-decoration: none;
  transition: all 0.3s;
}

.menu-item:hover {
  background-color: #34495e;
  color: white;
}

.menu-item.active {
  background-color: #4CAF50;
  color: white;
}

.menu-icon {
  margin-right: 15px;
  font-size: 18px;
}

.admin-sidebar-footer {
  padding: 20px;
  border-top: 1px solid #34495e;
  margin-top: auto;
}

.logout-btn {
  width: 100%;
  padding: 10px;
  background-color: #e74c3c;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.logout-btn:hover {
  background-color: #c0392b;
}

/* 管理员主内容区 */
.admin-main-content {
  flex: 1;
  padding: 20px 30px;
  max-width: 100%;
  overflow-x: auto;
}

.admin-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.admin-header h1 {
  margin: 0;
  color: #2c3e50;
}

.admin-user-info {
  color: #7f8c8d;
}

/* 统计卡片 */
.dashboard-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
}

.stat-icon {
  font-size: 32px;
  margin-right: 20px;
}

.stat-content h3 {
  margin: 0;
  font-size: 24px;
  color: #2c3e50;
}

.stat-content p {
  margin: 5px 0 0;
  color: #7f8c8d;
  font-size: 14px;
}

/* 最近活动 */
.recent-activities {
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.recent-activities h2 {
  margin: 0 0 20px;
  color: #2c3e50;
  font-size: 18px;
}

.activities-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.activity-item {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid #ecf0f1;
}

.activity-item:last-child {
  border-bottom: none;
}

.activity-time {
  color: #95a5a6;
  font-size: 14px;
}

.activity-content {
  color: #2c3e50;
}
</style>